package helperClasses;

/**
 * 
 * @author Felix Schliephacke
 * 
 */
public class Help {

	public static int[] calculateColumnIndices(int squareIndex, int squareSize)
			throws IllegalArgumentException {
		if (squareIndex < 0) {
			throw new IllegalArgumentException(
					"Square index must be greater than or equal to 0.");
		}
		if (squareSize <= 0) {
			throw new IllegalArgumentException("Size must be greater than 0.");
		}
		int size = squareSize * squareSize;
		if (squareIndex >= size) {
			throw new IllegalArgumentException(
					"Square index must be lower than " + size + ".");
		}
		int[] columnIndices = new int[squareSize];
		int offset = (squareIndex % squareSize) * squareSize;
		for (int i = 0; i < squareSize; i++) {
			columnIndices[i] = i + offset;
		}
		return columnIndices;
	}

	public static int[] calculateRowIndices(int squareIndex, int squareSize)
			throws IllegalArgumentException {
		if (squareIndex < 0) {
			throw new IllegalArgumentException(
					"Square index must be greater than or equal to 0.");
		}
		if (squareSize <= 0) {
			throw new IllegalArgumentException("Size must be greater than 0.");
		}
		int size = squareSize * squareSize;
		if (squareIndex >= size) {
			throw new IllegalArgumentException(
					"Square index must be lower than " + size + ".");
		}
		int[] rowIndices = new int[squareSize];
		int offset = (int) (squareIndex / squareSize) * squareSize;
		for (int i = 0; i < squareSize; i++) {
			rowIndices[i] = i + offset;
		}
		return rowIndices;
	}

	public static int calculateSquareIndex(int row, int column, int squareSize) {
		// TODO implement
		int rowOffset = (int) (row / squareSize) * squareSize;
		int columnOffset = column / squareSize;
		return rowOffset + columnOffset;
	}

	public static int countObjectOccurencesInArray(Object object, Object[] array) {
		int count = 0;
		for (Object temp : array) {
			if (temp.equals(object)) {
				count++;
			}
		}
		return count;
	}

	public static boolean isPositiveSquare(int arg)
			throws IllegalArgumentException {
		if (arg <= 0) {
			throw new IllegalArgumentException(
					"Argument must be greater than 0.");
		}
		for (int i = 1; i <= (int) Math.sqrt(arg); i++) {
			if (arg == i * i) {
				return true;
			}
		}
		return false;
	}
}
